package com.jinduplan.planmanager.app

import android.app.Activity
import android.app.Application
import android.content.Context
import android.os.Bundle
import android.os.Handler
import android.os.Vibrator
import android.view.Gravity
import androidx.multidex.MultiDex
import com.alibaba.android.arouter.launcher.ARouter
import com.baidu.mapapi.CoordType
import com.baidu.mapapi.SDKInitializer
import com.cxz.kotlin.baselibs.base.CrashHandler
import com.cxz.kotlin.baselibs.config.AppConfig
import com.cxz.kotlin.baselibs.utils.NLog
import com.fphoenixcorneae.util.toast.ToastUtil
import com.jinduplan.planmanager.service.LocationService
import com.squareup.leakcanary.LeakCanary
import com.squareup.leakcanary.RefWatcher
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors

/**
 * @author admin
 * @date 2018/11/21
 * @desc
 */
class App : Application() {

    private val TAG = "App"

    private var refWatcher: RefWatcher? = null

    val locationService: LocationService by lazy {
         LocationService(applicationContext)
    }
    var mVibrator: Vibrator? = null

    companion object {
        fun getRefWatcher(context: Context): RefWatcher? {
            val app = context.applicationContext as App
            return app.refWatcher
        }

        var context: Context? = null
        var singlePool: ExecutorService = Executors.newSingleThreadExecutor()
        var handler = Handler()

    }

    override fun onCreate() {
        super.onCreate()
        context = this
        initApp()
        initLeakCanary()
        initRouter()
    }

    private fun initApp() {
        AppConfig.init(this)
        AppConfig.openDebug()
        CrashHandler.Instance.init(this)
        ToastUtil.setGravity(Gravity.BOTTOM,0,40)
        //在使用SDK各组件之前初始化context信息，传入ApplicationContext
        SDKInitializer.initialize(this);
        //自4.3.0起，百度地图SDK所有接口均支持百度坐标和国测局坐标，用此方法设置您使用的坐标类型.
        //包括BD09LL和GCJ02两种坐标，默认是BD09LL坐标。
        SDKInitializer.setCoordType(CoordType.GCJ02);
    }

    private fun initLeakCanary() {
        refWatcher = if (LeakCanary.isInAnalyzerProcess(this))
            RefWatcher.DISABLED
        else LeakCanary.install(this)

        registerActivityLifecycleCallbacks(mActivityLifecycleCallbacks)
    }

    private fun initRouter() {
        ARouter.openLog()
        ARouter.openDebug()
        ARouter.init(this)
    }

    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        MultiDex.install(this)
    }

    private val mActivityLifecycleCallbacks = object : ActivityLifecycleCallbacks {
        override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
            NLog.d(TAG, "onCreated: " + activity.componentName.className)
        }

        override fun onActivityStarted(activity: Activity) {
            NLog.d(TAG, "onStart: " + activity.componentName.className)
        }

        override fun onActivityResumed(activity: Activity) {
            NLog.d(TAG, "onResume: " + activity.componentName.className)
        }

        override fun onActivityPaused(activity: Activity) {
            NLog.d(TAG, "onPause: " + activity.componentName.className)
        }

        override fun onActivityStopped(activity: Activity) {
            NLog.d(TAG, "onStop: " + activity.componentName.className)
        }

        override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
            NLog.d(TAG, "onSaveInstanceState: " + activity.componentName.className)
        }

        override fun onActivityDestroyed(activity: Activity) {
            NLog.d(TAG, "onDestroy: " + activity.componentName.className)
        }
    }

    //    @Override
    //    public void onCreate() {
    //        super.onCreate();
    //        MultiDex.install(this);
    //        context = getApplicationContext();
    //        registerActivityLifecycleCallbacks(callback);
    //        IoTSmart.init(this);
    //    }


}